//内容页JS
window.onload = function(){
    //console.log(123);
    //获取参数softaware-html/content.html?aid=7&cid=20
    let laysize = getLoctionParam('aid');
    //判断
    if(!isNaN(parseInt(laysize))&&laysize>0){
        console.log('235');
        $.ajax({
            url:'https://cache.iminutes.cn/api/soft/detail.html',
            type:"POST",
            data:{
                laysize:laysize
            },
            dataType:'json',
            success:(res)=>{
                //console.log(res);
                let {data:article} = res;
                //console.log(article);
                if(res.status==200){
                    document.querySelector('.article_title_lmt').innerHTML= article.title;
                    document.querySelector('#news_content').innerHTML=article.content;
                    document.querySelector('.author').innerHTML=article.author;
                    document.querySelector('.addtime').innerHTML=article.newtime;
                    document.querySelector('.viewnum').innerHTML=article.viewnum+'次';
                    //判断是否是收藏了
                    console.log(article.isopinion);
                    if(article.isopinion==0){
                        //没收藏
                        document.querySelector('#heartbox').className='fa fa-heart-o fa-2x';
                    }else{
                        document.querySelector('#heartbox').className='fa fa-heart fa-2x cur';
                    }
                    //给小星星设置自定义属性
                    document.querySelector('#heartbox').setAttribute('laysize',article.aid);
                    document.querySelector('#heartbox').setAttribute('isopinion',article.isopinion);
                }else{
                    alert('请求失败!');
                }
            }
        });

    }else{
        console.log('参数无效0,-1,没传值...让你回退到list列表页');
        window.location.href = 'list.html';
    }


    //给小星星绑定点击事件
    document.querySelector('#heartbox').addEventListener('click',function(e){
        //console.log('123');
        //通过自定义属性获取文章的ID
        let laysize = this.getAttribute('laysize');
        //是否收藏标志
        let isopinion = this.getAttribute('isopinion');
        console.log(laysize+'=='+isopinion);
        //获取的isopinion是0，就改成1 --------1，改成0
        $.ajax({
            url:'https://cache.iminutes.cn/api/soft/heart.html',
            type:"POST",
            data:{
                laysize:laysize
            },
            dataType:'json',
            success:(res)=>{
                //console.log(res);
                if(res.status==200){
                    if(res.data.isopinion==0){
                        document.querySelector('#heartbox').className='fa fa-heart-o fa-2x';
                    }else{
                        document.querySelector('#heartbox').className='fa fa-heart fa-2x cur';
                    }
                }else{
                    alert('请求失败!');
                }
            }
        });



    });


}